package com.lx.huimail.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lx.huimail.MyApplication;
import com.lx.huimail.R;
import com.lx.huimail.configs.Config;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import dmax.dialog.SpotsDialog;
import okhttp3.Call;

public class LoginActivity extends Activity {
	private TextView forgetpassword;
	private Button login_button1;
	private EditText account;
	private EditText pwd;
	private SpotsDialog mDialog;
	private CheckBox rem_pw, auto_login;
	private SharedPreferences sp,sp1;
	private String tempPwd,tempAccount;
	private String out;
	public static Activity instance;
	String isvalid,custtypeid;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_login);
		instance=this;
		InitView();
		//获得实例对象
		sp = LoginActivity.this.getSharedPreferences("userInfo", Context.MODE_PRIVATE);

       //判断记住密码多选框的状态
		if (sp.getBoolean("ISCHECK", false)) {
			//设置默认是记录密码状态
			rem_pw.setChecked(true);
			account.setText(sp.getString("USER_NAME", ""));
			pwd.setText(sp.getString("PASSWORD", ""));
			//判断自动登陆多选框状态
			if (sp.getBoolean("AUTO_ISCHECK", false)) {
				//设置默认是自动登录状态
				auto_login.setChecked(true);
			}
		}

			forgetpassword.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
					intent.putExtra("hello","2");
					startActivity(intent);

				}
			});
			//监听记住密码多选框按钮事件
			rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (rem_pw.isChecked()) {

						System.out.println("记住密码已选中");
						sp.edit().putBoolean("ISCHECK", true).commit();

					} else {

						System.out.println("记住密码没有选中");
						sp.edit().putBoolean("ISCHECK", false).commit();

					}

				}
			});

			//监听自动登录多选框事件
			auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (auto_login.isChecked()) {
						System.out.println("自动登录已选中");
						sp.edit().putBoolean("AUTO_ISCHECK", true).commit();

					} else {
						System.out.println("自动登录没有选中");
						sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
					}
				}
			});

			//登陆监听
			login_button1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					tempAccount = account.getText().toString().trim();
					tempPwd = pwd.getText().toString().trim();

					if (tempAccount.isEmpty() && tempPwd.isEmpty()) {
						Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
					} else {
						mDialog = new SpotsDialog(LoginActivity.this, "正在登陆....");
						mDialog.show();
						HashMap<String, String> map = new HashMap<String, String>();
						map.put("account", tempAccount);
						map.put("password", tempPwd);
						Gson gson = new Gson();
						String parmas = gson.toJson(map).toString();
						OkHttpUtils
								.post()
								.url(Config.url + "login/appLogin.do")
								.addParams("data", parmas)
								.build()
								.execute(new StringCallback() {
									@Override
									public void onError(Call call, Exception e, int id) {
										Toast.makeText(LoginActivity.this, "链接服务器失败", Toast.LENGTH_SHORT).show();
									}

									@Override
									public void onResponse(String response, int id) {
										//[{"createtime":1476670928000,"custtypeid":0,"id":84,"isvalid":1,"password":"f8:a0:47:cd:fd:cc:3d:df","phone":"18865734190","picname":"147735615650182tempfile","picsrc":"header/"}]

										mDialog.dismiss();
										JSONArray jsonArray = null;
										JSONObject jsonObject = null;
										try {
											jsonArray = new JSONArray(response);
											jsonObject = jsonArray.getJSONObject(0);

											if (jsonObject.length() > 2) {
												isvalid=jsonObject.getString("isvalid");
												custtypeid=jsonObject.getString("custtypeid");
												String custid = jsonObject.getString("id");
												SharedPreferences preferences = getSharedPreferences("user", Context.MODE_PRIVATE);
												Editor editor = preferences.edit();
												editor.putString("custid", custid);
												editor.putBoolean("status", true);
												editor.commit();
												//登陆成功后发送一个广播
												//Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
												if (rem_pw.isChecked()) {
													//记住用户名、密码、
													Editor editor1 = sp.edit();
													editor1.putString("USER_NAME", tempAccount);
													editor1.putString("PASSWORD", tempPwd);
													editor1.commit();
												}
												if (isvalid.equals("0")){
													Toast.makeText(LoginActivity.this,"您的账号已失效",Toast.LENGTH_LONG).show();
												}else if (!custtypeid.equals("1")){
													Toast.makeText(LoginActivity.this,"账号非配送端账号",Toast.LENGTH_LONG).show();
												}else {
													Intent intent = new Intent(LoginActivity.this, MainFragmentActivity.class);
													startActivity(intent);
													finish();
												}
											} else {
												Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
											}
										} catch (JSONException e) {
											e.printStackTrace();
										}

									}
								});
					}

				}
			});

		}

	private void InitView(){
		account=(EditText)findViewById(R.id.et_zhanghao);
		pwd=(EditText)findViewById(R.id.et_mima);
		login_button1=(Button)findViewById(R.id.login_button1);
		forgetpassword=(TextView)findViewById(R.id.forget_pwd);
		rem_pw = (CheckBox) findViewById(R.id.cb_mima);
		auto_login = (CheckBox) findViewById(R.id.cb_auto);

	}
}

